/*
 * Copyright (c) 2001, 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.core.internal.api;

import java.util.ArrayList;

import net.sourceforge.perforce.api.IPerforceCommand;
import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.api.IResolvedResult;
import net.sourceforge.perforce.core.syncinfo.ResourceSyncInfo;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Path;

/**
  * ClientUser object for the 'p4 resolved' command.
  * 
  * @version $Revision: 252 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class Resolved extends AbstractPerforceCommand implements IPerforceCommand
{
	// --------------------------------------------------------------------------- Private Variables
	
	/** Stores the command's status. */
	private MultiStatus status;
	/** Stores the result of the 'p4 resolved' operation. */
	private ArrayList resultList = new ArrayList();

	// --------------------------------------------------------------- Public Static Factory Methods
	
	/** Convenience method for creating instances of 'Resolved'. 
	  * 
	  * @return the new Resolved instance.
	  */
	public static final Resolved newInstance()
	{
		MultiStatus status = new MultiStatus(PerforceCorePlugin.ID, IStatus.OK, "", null);			
		return new Resolved(status);
	}

	// ------------------------------------------------------------------------ Private Constructors
	
	/** Constructor for Resolved. 
	  * 
	  * @param status the MultiStatus object.
	  */
	private Resolved(MultiStatus status)
	{
		super(status);
		this.status = status;
	}

	// -------------------------------------------------------- Public Methods (IClientUserDelegate)
	
	/** @see IClientUserDelegate#outputInfo(char, String) */
	public void outputInfo(char level, String infoString) 
	{
		if (level != '0')
		{
			return;
		}
		
		int start = infoString.indexOf(" - branch from ");                             //$NON-NLS-1$
		if (start != -1)
		{
			String depotFilename = infoString.substring(start + 15);
			String filename = infoString.substring(0, start);
		
			Result result = new Result(depotFilename, filename, ResourceSyncInfo.ACTION_BRANCH);
			resultList.add(result);
			
			return;
		}
		
		start = infoString.indexOf(" - ");                                             //$NON-NLS-1$
		if (start != -1)
		{
			int realStart = infoString.indexOf("//", start + 3);                       //$NON-NLS-1$
			String depotFilename = infoString.substring(realStart);
			String filename = infoString.substring(0, start);
			
			Result result = new Result(depotFilename, filename, "");                   //$NON-NLS-1$
			resultList.add(result);
			
			return;
		}
		
		super.outputInfo(level, infoString);
	}
			
	// ----------------------------------------------------------- Public Methods (IPerforceCommand)
	
	/** @see IPerforceCommand#getCommand() */
	public String getCommand() 
	{
		return "resolved";                                                             //$NON-NLS-1$
	}

	/** @see IPerforceCommand#getStatus() */
	public IStatus getStatus() 
	{
		return status;
	}

	// ------------------------------------------------------------------------------ Public Methods
	
	/**  Returns the operation's result.
	  * 
	  * @return the result of 'p4 resolved'. 
	  */
	public IResolvedResult[] getResult()
	{
		return (IResolvedResult[]) resultList.toArray(new IResolvedResult[resultList.size()]);
	}
	
	// ---------------------------------------------------------------------- Private Nested Classes
	
	/** Class representing the operations result. */
	private static class Result implements IResolvedResult
	{
		// ----------------------------------------------------------------------- Private Variables
		
		/** Stores the depot filename */
		private String depotFilename;
		/** Stores the location */
		private IPath location;
		/** Stores the action. Currently only "branch" or "" */
		private String action;
		
		// --------------------------------------------------------------------- Public Constructors
		
		/** Constructor for Result.
		  *
		  * @param theDepotFilename the depot filename.
		  * @param theFilename the filename.
		  * @param theAction the action. 
		  */		
		public Result(String theDepotFilename, String theFilename, String theAction)
		{
			depotFilename = theDepotFilename.substring(0, theDepotFilename.indexOf('#'));
			location = new Path(theFilename);
			action = theAction;
		}
		
		// -------------------------------------------------------------------------- Public Methods
		
		/** @see net.sourceforge.perforce.core.api.IResolvedResult#getDepotFilename() */
		public String getDepotFilename()
		{
			return depotFilename;
		}
		
		/** @see net.sourceforge.perforce.core.api.IResolvedResult#getLocation() */
		public IPath getLocation()
		{
			return location;
		}

		/** @see net.sourceforge.perforce.core.api.IResolvedResult#getAction() */
		public String getAction()
		{
			return action;
		}
	}
}
